﻿Ext.define('SmartClient.controller.OrdersHistoryController',
   {
      extend: 'Deft.mvc.ViewController',

      init: function ()
      {
         this.getView().down('gridpanel').addListener('resize', SmartClient.utils.CommonFunctions.setWindowSizeAndPosition,
            this.getView(), 'ordersHistoryWindow');

         return this.callParent(arguments);
      },


      editOrder: function (grid, rowIndex, colIndex, item, event, record)
      {
         if (record.raw.products.length > 0)
         {
            var productsStore = Ext.create('SmartClient.store.ProductsStore',
                {
                   data: record.raw.products
                }),
                orderId = record.get('id');
            if (!Ext.getCmp('addedProductsWindow'))
            {
               Ext.create('SmartClient.view.AddedProductsView',
                  {
                     store: productsStore,
                     orderId: orderId
                  }).show();
               this.getView().close();
               
               var productsController = Ext.getCmp('productsPanel').getController(),
                   addedProductsCookie = SmartClient.Application.ADDED_PRODUCTS_COOKIE,
                   addedProductsDelimiter = SmartClient.Application.ADDED_PRODUCTS_DELIMITER,
                   addedProductsCookieValue = '';
               SmartClient.utils.CookieUtils.eraseCookie(addedProductsCookie);

               Ext.each(record.raw.products, function (product, index)
               {
                  addedProductsCookieValue += (SmartClient.controller.ProductsController.
                     generateAddedProductFromTemplate(product.id, product.orderedAmount) + addedProductsDelimiter);
               });
               // substring to remove last products delimiter
               SmartClient.utils.CookieUtils.setCookie(addedProductsCookie, addedProductsCookieValue.substring(0, addedProductsCookieValue.length - 1), 30);
               SmartClient.utils.CookieUtils.setCookie(SmartClient.Application.EDITED_ORDER_COOKIE, orderId, 30);
               productsController.refreshViewAddedProductsButtonText();
            }
         }
      },

      rejectOrder: function (grid, rowIndex, colIndex, item, event, record)
      {
         var me = this;
         Ext.Ajax.request(
            {
               url: 'rejectOrder',
               method: 'POST',
               headers: { 'Content-Type': 'application/json' },
               params: Ext.JSON.encode(
                  {
                     // Customer model
                     id: SmartClient.utils.CookieUtils.getCookieValue(SmartClient.Application.USER_COOKIE),
                     orders:
                        [
                           { id: record.get('id') }
                        ]
                  }),
               success: function (conn, response, options, eOpts)
               {
                  var store = me.getView().store;
                  store.removeAll();
                  store.load();
               },
               failure: function (conn, response, options, eOpts)
               {
                  
               }
            });
      }
   });